import java.util.Iterator;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;
import org.eclipse.ui.forms.events.ExpansionEvent;
import org.eclipse.ui.forms.events.IExpansionListener;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;


public class SettingsSections {

	private Shell shell;
	private TabFolder tabTypeViewSettings;
	private Property dataSuppliers;
	private ScrolledComposite verticalScrollForSections;

	public SettingsSections(Shell shell, TabFolder tabTypeViewSettings, Property dataSuppliers) {
		this.shell = shell;
		this.tabTypeViewSettings = tabTypeViewSettings;
		this.dataSuppliers = dataSuppliers;
	}

	public void createContents(){
		createTabItemSection();
		FormToolkit toolKit = new FormToolkit(shell.getDisplay());
		Composite composite1 = createComposite();
		Composite composite2 = createComposite();
		createSections(toolKit, composite1, composite2);
	}

	private void createSections(FormToolkit toolKit, Composite composite1,
			Composite composite2) {
		String supplier = "";
		Section section = null;
		Composite client = null;
		int colSupplier = getcolSuppliers();
		int colSection = 0;
		Iterator<String> itr = dataSuppliers.getKeysSort().iterator();
		while(itr.hasNext()){
			String key = itr.next();
			String suppl = key.substring(0, key.indexOf(".") - 1);
			if (!supplier.equals(suppl)){
				supplier = suppl;
				Composite mainComposite;
				if (colSection <= colSupplier / 2){
					mainComposite = composite1;
				} else {
					mainComposite = composite2;
				}
				colSection++;
				section = toolKit.createSection(mainComposite, 
						Section.LEFT_TEXT_CLIENT_ALIGNMENT | Section.TWISTIE);
				section.addExpansionListener(new IExpansionListener() {
					@Override
					public void expansionStateChanging(ExpansionEvent e) {
					}

					@Override
					public void expansionStateChanged(ExpansionEvent e) {
						verticalScrollForSections.setMinSize(((Composite)verticalScrollForSections.getContent()).computeSize(SWT.DEFAULT, SWT.DEFAULT));
					}
				});
				section.setText(supplier);
				section.setBackground(getParentColor(section));
				client = createCompositeSection(toolKit, section);
			}
			SetCompositeSection(toolKit, client, key);
			toolKit.paintBordersFor(client);
			section.setClient(client);
		}
	}

	private void SetCompositeSection(FormToolkit toolKit, Composite client,
			String key) {
		String setting = key.substring(key.indexOf(".") + 1);
		String value = dataSuppliers.getDataSettings().getProperty(key);
		Label label = toolKit.createLabel(client, setting);
		label.setBackground(label.getParent().getBackground());
		Text t = toolKit.createText(client, value);
		GridData gd = new GridData(GridData.FILL_BOTH);
		gd.heightHint = 20;
		gd.widthHint = 150;
		t.setLayoutData(gd);
	}

	private Composite createCompositeSection(FormToolkit toolKit,
			Section section) {
		Composite client;
		client = toolKit.createComposite(section, SWT.NONE);
		client.setBackground(getParentColor(client));
		GridLayout layout = new GridLayout(2, false);
		layout.marginWidth = 20;
		layout.marginHeight = 20;
		client.setLayout(layout);
		return client;
	}

	private void createTabItemSection() {
		verticalScrollForSections = createVerticalScroll(tabTypeViewSettings);
		TabItem item = new TabItem (tabTypeViewSettings, SWT.NONE);
		item.setText ("Sections");
		Composite composite = new Composite(verticalScrollForSections, SWT.BORDER | SWT.MULTI);
		composite.setLayout(new GridLayout(2, true));
		composite.setBackground(getParentColor(composite));
		item.setControl(verticalScrollForSections);
		verticalScrollForSections.setContent(composite);
		verticalScrollForSections.setExpandHorizontal(true);
		verticalScrollForSections.setExpandVertical(true);
		verticalScrollForSections.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		verticalScrollForSections.setShowFocusedControl(true);
	}

	private Composite createComposite() {
		Composite composite = new Composite((Composite)((ScrolledComposite)tabTypeViewSettings.getItem(0).getControl()).getContent(), SWT.NONE);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true));
		composite.setLayout(new GridLayout());
		composite.setBackground(getParentColor(composite));
		return composite;
	}

	private int getcolSuppliers() {
		int colSupplier = 0;
		String supplier = "";
		Iterator<String> itr = dataSuppliers.getKeysSort().iterator();
		while(itr.hasNext()){
			String key = itr.next();
			String suppl = key.substring(0, key.indexOf(".") - 1);
			if (!supplier.equals(suppl)){
				supplier = suppl;
				colSupplier++;
			}
		}
		return colSupplier;
	}

	private Color getParentColor(Composite child) {
		return child.getParent().getBackground();
	}

	private ScrolledComposite createVerticalScroll(Composite parent) {
		ScrolledComposite sc = new ScrolledComposite(parent, SWT.BORDER |  SWT.V_SCROLL);
		sc.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		return sc;
	}


}
